package cn.example.notificationtest;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.media.AudioAttributes;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import java.io.File;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button sendNotice = findViewById(R.id.send_notice);
        sendNotice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String channelId = createNotificationChannel("myChannelId", "myChannelName", NotificationManager.IMPORTANCE_MAX);
                NotificationManager notificationManager = (NotificationManager) v.getContext().getSystemService(NOTIFICATION_SERVICE);
                Intent intent = new Intent(v.getContext(), NotificationActivity.class);
                PendingIntent pi = PendingIntent.getActivity(v.getContext(), 0, intent, PendingIntent.FLAG_IMMUTABLE);
                Notification notification = new NotificationCompat.Builder(v.getContext(), channelId)
                        .setContentTitle("This is a Notice")
//                        .setContentText("This is notice content")
                        .setWhen(System.currentTimeMillis())
                        .setSmallIcon(R.drawable.ic_launcher_background)
                        .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_foreground))
                        .setContentIntent(pi)
//                        .setAutoCancel(true)
                        .setStyle(new NotificationCompat.BigPictureStyle().bigPicture
                                (BitmapFactory.decodeResource(getResources(), R.drawable.img1)))
                        .build();
                notificationManager.notify(1, notification);
            }
        });
    }

    private String createNotificationChannel(String channelId, String channelName, int level){
        if(Build.VERSION.SDK_INT > Build.VERSION_CODES.O){
            NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            NotificationChannel channel = new NotificationChannel(channelId, channelName, level);
            channel.setSound(Uri.fromFile(new File("/system/media/audio/ringtones/Luna.ogg")), Notification.AUDIO_ATTRIBUTES_DEFAULT);
            channel.enableLights(true);
            channel.setLightColor(Color.GREEN);
            channel.enableVibration(true);
            channel.setVibrationPattern(new long[]{0, 1000, 1000, 1000});
            manager.createNotificationChannel(channel);
            return channelId;
        }else {
            return null;
        }
    }
}